package cn.jboa.dao.impl;

import cn.jboa.common.Constants;
import cn.jboa.dao.EmployeeDao;
import cn.jboa.entity.Employee;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository("employeeDao")
public class EmployeeDaoImol extends BaseHibernateDaoSupport<Employee> implements EmployeeDao {

    @Autowired
    public EmployeeDaoImol(@Qualifier("sessionFactory") SessionFactory sessionFactory) {
        this.setSessionFactory(sessionFactory);
    }

    public List<Employee> findEmployeeByProperty(String sn, Object value) {
        String queryString = "from Employee e where e.sn= ?";
        return this.getHibernateTemplate().find(queryString, value);
    }

    public Employee getManager(Employee employee) {
        String hql = "from Employee e where e.status = ? and " +
                "e.sysDepartment.id = ? and e.sysPosition.nameCn = ?";
        List<Employee> emps = this.find(hql, Constants.EMPLOYEE_STAY, employee
                .getSysDepartment().getId(), Constants.POSITION_FM);
        if (emps == null || emps.size() == 0)
            return null;
        return emps.get(0);
    }
}
